﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO;
using System.Web;
using System.Configuration;

namespace VConfig
{
    /// <summary>
    /// 网站常量统一设置
    /// </summary>
    public static class WebConstConfig
    {
        #region 私有

        /// <summary>
        /// 配置文件管理类
        /// </summary>
        static ConfigFilesManager cfm;
        /// <summary>
        /// 系统计时器
        /// </summary>
        static Timer baseConfigTimer = new Timer(15000);
        /// <summary>
        /// 基本设置描述类
        /// </summary>
        static WebConst baseConfig;
        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        static string fileName = null;
        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        static string ConfigFilePath
        {
            get
            {
                if (fileName == null)
                {
                    HttpContext context = HttpContext.Current;
                    string siteVirtrualPath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SiteVirtrualPath"]) ?
                        "/" : ConfigurationManager.AppSettings["SiteVirtrualPath"];
                    if (context != null)
                    {
                        fileName = context.Server.MapPath(string.Format("{0}/Configs/WebConstConfig.Config", siteVirtrualPath));
                    }
                    else
                    {
                        fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Configs\WebConstConfig.Config");
                    }

                    if (!File.Exists(fileName))
                    {
                        throw new Exception("发生错误: 网站" +
                            new FileInfo("fileName").DirectoryName
                            + "目录下没有正确的WebConstConfig.Config文件");
                    }
                }
                return fileName;
            }
        }

        #endregion

        #region 公开的属性

        /// <summary>
        /// 默认国际化语言编码
        /// </summary>
        public static string DefaultCultureID { get { return baseConfig.DefaultCultureID; } }
        /// <summary>
        /// EmailSmtp
        /// </summary>
        static public string EmailSmtp { get { return baseConfig.EmailSmtp; } }

        /// <summary>
        /// 发送邮件用户名
        /// </summary>
        static public string EmailUser { get { return baseConfig.EmailUser; } }

        /// <summary>
        /// 发送邮件密码
        /// </summary>
        static public string EmailPwd { get { return baseConfig.EmailPwd; } }

        /// <summary>
        /// 第一域名
        /// </summary>
        public static string DoMain { get { return baseConfig.DoMain; } }

        /// <summary>
        /// 第二域名
        /// </summary>
        public static string DoMainSecond { get { return baseConfig.DoMainSecond; } }
        /// <summary>
        /// 网站名称
        /// </summary>
        public static string WebSiteName { get { return baseConfig.WebSiteName; } }
        /// <summary>
        /// 本地文件上传的路径
        /// </summary>
        public static string LocationUploadPath { get { return baseConfig.LocationUploadPath; } }
        /// <summary>
        /// 默认上传到文件服务器的URI（上传的，可以用WS或者FTP等方式）
        /// </summary>
        public static string DefaultUploadUri { get { return baseConfig.DefaultUploadUri; } }
        /// <summary>
        /// 图像文件服务器地址（展现的）
        /// </summary>
        public static string ImageServerHost { get { return baseConfig.ImageServerHost; } }

        #endregion

        #region 构造函数

        /// <summary>
        /// 静态构造函数初始化相应实例和定时器
        /// </summary>
        static WebConstConfig()
        {
            cfm = new ConfigFilesManager(ConfigFilePath, typeof(WebConst));
            baseConfig = (WebConst)cfm.LoadConfig();
            baseConfigTimer.AutoReset = true;
            baseConfigTimer.Enabled = true;
            baseConfigTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            baseConfigTimer.Start();
        }

        #endregion

        #region 重设配置类实例
        /// <summary>
        /// 重设配置类实例
        /// </summary>
        public static void ResetConfig()
        {

            baseConfig = (WebConst)cfm.LoadRealConfig();
        }

        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ResetConfig();
        }
        #endregion
    }
}
